package com.raidpoints.client;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;

public class AccountPermBox extends DialogBox {
	RaidPoints rp;
	int cid;
	ListBox groupBox;
	ListBox roleBox;
	ArrayList<Integer> roleid;
	ArrayList<Integer> groupid;
	
	public AccountPermBox(RaidPoints mrp, String name, int mcid) {
		rp = mrp;
		cid = mcid;
		setHTML("Account Permissions: " + name);
		
		roleid = new ArrayList<Integer>();
		groupid = new ArrayList<Integer>();
		
		LayoutPanel layoutPanel = new LayoutPanel();
		setWidget(layoutPanel);
		layoutPanel.setSize("240px", "204px");
		
		Button btnSave = new Button("Save");
		layoutPanel.add(btnSave);
		layoutPanel.setWidgetLeftWidth(btnSave, 22.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(btnSave, 164.0, Unit.PX, 28.0, Unit.PX);
		
		Button btnClose = new Button("Close");
		layoutPanel.add(btnClose);
		layoutPanel.setWidgetLeftWidth(btnClose, 129.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(btnClose, 164.0, Unit.PX, 28.0, Unit.PX);
		
		Label lblRole = new Label("Role:");
		layoutPanel.add(lblRole);
		layoutPanel.setWidgetLeftWidth(lblRole, 18.0, Unit.PX, 49.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblRole, 17.0, Unit.PX, 16.0, Unit.PX);
		
		roleBox = new ListBox();
		roleBox.setWidth("160px");
		layoutPanel.add(roleBox);
		layoutPanel.setWidgetLeftWidth(roleBox, 63.0, Unit.PX, 230.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(roleBox, 9.0, Unit.PX, 24.0, Unit.PX);
		
		Label lblCharters = new Label("Groups:");
		layoutPanel.add(lblCharters);
		layoutPanel.setWidgetLeftWidth(lblCharters, 18.0, Unit.PX, 56.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblCharters, 44.0, Unit.PX, 16.0, Unit.PX);
		
		groupBox = new ListBox(true);
		groupBox.setWidth("205px");
		layoutPanel.add(groupBox);
		layoutPanel.setWidgetLeftWidth(groupBox, 18.0, Unit.PX, 225.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(groupBox, 66.0, Unit.PX, 82.0, Unit.PX);
		groupBox.setVisibleItemCount(5);
		
		btnClose.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				hide();				
			}
		});
		
		btnSave.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				//roleid[,groupid1,groupid2,...]
				String tbs = Integer.toString(roleid.get(roleBox.getSelectedIndex()));
				if(tbs.equals("3")){
					for(int i = 0; i < groupBox.getItemCount(); i++){
						if(groupBox.isItemSelected(i)) tbs = tbs + "," + Integer.toString(groupid.get(i));
					}
				}
				rp.rpsvc.SendAccountPerm(cid, tbs, rp.stringCallback);
				hide();				
			}
		});
		
		roleBox.addChangeHandler(new ChangeHandler(){

			@Override
			public void onChange(ChangeEvent event) {
				if(roleid.get(roleBox.getSelectedIndex()) == 3) groupBox.setEnabled(true);
				else{
					for(int i = 0; i < groupBox.getItemCount(); i++) groupBox.setItemSelected(i, false);
					groupBox.setEnabled(false);
				}
			}
			
		});
		
		AsyncCallback<ArrayList<String>> permCallback = new AsyncCallback<ArrayList<String>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ArrayList<String> result) {
				if(result == null) return;
				roleid.clear(); groupid.clear();
				int rsel = -1;
				String[] roles = result.get(0).split(",");	
				String[] roleseq = result.get(1).split(",");
				for(int i = 0; i < roles.length; i++){
					roleid.add(Integer.parseInt(roles[i].substring(0,roles[i].indexOf("__"))));
					roleBox.addItem(roles[i].substring(roles[i].indexOf("__")+2));
				}
				for(int i = 0; i < roleseq.length; i++){
					if(roleseq[i].equals("1")) {
						rsel = roleid.get(i);
						roleBox.setSelectedIndex(i);
						break;
					}
				}

				roles = result.get(2).split(",");	
				roleseq = result.get(3).split(",");
				for(int i = 0; i < roles.length; i++){
					groupid.add(Integer.parseInt(roles[i].substring(0,roles[i].indexOf("__"))));
					groupBox.addItem(roles[i].substring(roles[i].indexOf("__")+2));
				}
				for(int i = 0; i < roleseq.length; i++){
					if(roleseq[i].equals("1")) {
						groupBox.setItemSelected(i, true);
						break;
					}
				}
				
				if(rsel != 3) groupBox.setEnabled(false);
				if(rsel == 1 && result.size() < 5) roleBox.setEnabled(false);
			}
		
			
		};
		
		rp.rpsvc.GetAccountPerm(cid, permCallback);
	}
}
